//
//  OilMarketListTitleHeadView.m
//  Base_App
//
//  Created by Yutian Duan on 2019/6/24.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "OilMarketListTitleHeadView.h"

@interface  OilMarketListTitleHeadView ()

@end

@implementation OilMarketListTitleHeadView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
  self = [super initWithReuseIdentifier:reuseIdentifier];
  if (self) {
    self.userInteractionEnabled = YES;
    self.backgroundColor = DEF_GRAY;
    [self createViews];
  }
  return self;
}

- (void)createViews {
  
  _logoImageView = [[UIImageView alloc] init];
  _logoImageView.contentMode = UIViewContentModeScaleAspectFit;
  [self addSubview:_logoImageView];
  
  _titleLabel = [[UILabel alloc] init];
  _titleLabel.textAlignment = NSTextAlignmentLeft;
  _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
  _titleLabel.textColor = DEF_BLACK;
  [self addSubview:_titleLabel];
  
  _timeLabel = [[UILabel alloc] init];
  _timeLabel.font = [UIFont systemFontOfSize:10];
  _timeLabel.textColor = [UIColor lightGrayColor];
  [self addSubview:_timeLabel];
  
  _arrowViewContent = [[UIView alloc] init];
  [self addSubview:_arrowViewContent];
  
  _arrowViewContent.userInteractionEnabled = YES;
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reloadTableViewClick)];
  [_arrowViewContent addGestureRecognizer:tap];

  
  _arrowButton = [[UIButton alloc] init];
  _arrowButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
  [_arrowButton setImage:[UIImage imageNamed:@"Oil_Market_Arrow"] forState:UIControlStateNormal];
  [_arrowButton addTarget:self action:@selector(reloadTableViewClick) forControlEvents:UIControlEventTouchUpInside];
  [self addSubview:_arrowButton];
  
  UIView *lineView = [[UIView alloc] init];
  lineView.backgroundColor = COLOR_SEPARATOR;
  [self addSubview:lineView];
  
  [_logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.mas_equalTo(self.mas_leading).offset(30);
    make.top.mas_equalTo(self.mas_top).offset(10);
    make.bottom.mas_equalTo(self.mas_bottom).offset(-10);
    make.width.mas_equalTo(self.logoImageView.mas_height);
  }];
  
  [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.mas_equalTo(self.logoImageView.mas_trailing).offset(10);
    make.top.mas_equalTo(self.mas_top).offset(10);
    make.bottom.mas_equalTo(self.mas_bottom).offset(-10);
    make.width.mas_equalTo(@85);
  }];
  
  [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.mas_equalTo(self.titleLabel.mas_trailing).offset(0);
    make.trailing.mas_equalTo(self.mas_trailing).offset(-50);
    make.height.mas_equalTo(20);
    make.bottom.mas_equalTo(self.titleLabel.mas_bottom);
  }];
  
  [_arrowViewContent mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.mas_equalTo(self.mas_centerY);
    make.trailing.mas_equalTo(self.mas_trailing).offset(-10);
    make.leading.mas_equalTo(self.timeLabel.mas_trailing);
    make.height.mas_equalTo(self.mas_height);
  }];
  
  [_arrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.mas_equalTo(self.mas_centerY);
    make.trailing.mas_equalTo(self.mas_trailing).offset(-30);
    make.width.mas_equalTo(@8);
    make.height.mas_equalTo(@10);
  }];
  
  [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.mas_equalTo(self.mas_leading).offset(20);
    make.trailing.mas_equalTo(self.mas_trailing).offset(-20);
    make.bottom.mas_equalTo(self.mas_bottom);
    make.height.mas_equalTo(@0.5);
  }];
  
}

- (void)setLayout:(OilMarktLayout *)layout {
  _layout = layout;
  
  _logoImageView.image = layout.logoImage;
  
  _titleLabel.text = layout.title;
  
  if (layout.updateTime.length > 0) {
    _timeLabel.hidden = NO;
    _timeLabel.text = layout.updateTime;
  } else {
    _timeLabel.hidden = YES;
  }
  
  if (layout.isOpen) {
    
  } else {
    
  }
    
}

- (void)reloadTableViewClick {

  if (self.reloadBlock) {
    self.reloadBlock();
  }

}

- (void)layoutSubviews {
  [super layoutSubviews];
  [_arrowButton layoutIfNeeded];
}

- (void)didMoveToSuperview {
  _arrowButton.imageView.transform = _layout.isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
}



@end
